<?php

// check user login
session_start();
if (isset($_SESSION["user"]) && !empty($_SESSION["user"])) {
//echo $_SESSION["user"];

//include 'function_library.php';


class Main extends CI_Controller {

	public function __construct() {
            parent::__construct(); 

            $this->load->helper(array('form','url'));
            $this->load->library('form_validation');
	}////// 
        ////////////////////////////////////////////////
        
        public function index() {
            $user_id = $_SESSION["user"];
            $user = $this->doctrine->em->find('ORM\Dynasties2\Users', $user_id);
            
            $data['firstname'] = $user->getFirstName();
            $data['user_id'] = $user->getID();
            $data['email'] = $user->getEmail();
            
            $this->load->library('session');
            $data['session_id'] = $this->session->userdata('session_id');
            
            
            
            if ($user_id == '1') {
                echo form_open('admin/index');
                echo form_submit('submit','Admin');
                echo form_close();
            }
            
            $this->load->model('query_models');
            $characters = $this->query_models->how_many_controlled_characters();
            //$this->load->view('MAIN_available_characters_view', $my_data);
            
            if ($characters) {
                $data['character_data'] = $characters;
            } else {
                
            }
            $this->load->view('MAIN_global_view',$data);
            
            
        }  ////// end of function index /////////////////////////
        /////////////////////////////////////////////////////////
        
        public function display_available_characters() {
            $user_id = $_SESSION["user"];
            $has_character = $this->doctrine->em->getRepository('ORM\Dynasties2\Characters')->findOneBy(array('playerControlled' => $user_id));
            
            if($has_character) {
                echo "You're not supposed to be there!</br>";
                $this->index();
            } else {
                $this->load->model('display_family_model');
                $my_data['data'] = $this->display_family_model->show_available_characters_for_play();
                $this->load->view('MAIN_available_characters_view', $my_data);
            }
            
        }  ////// end of function display_available_characters
        ////////////////////////////////////////////////
        
        public function display_my_titles() {
            $user_id = $_SESSION["user"];
            $has_character = $this->doctrine->em->getRepository('ORM\Dynasties2\Characters')->findOneBy(array('playerControlled' => $user_id));
            
            if($has_character) {
                echo "You're not supposed to be there!</br>";
                $this->index();
            } else {
                $this->load->model('display_family_model');
                $my_data['data'] = $this->display_family_model->show_available_characters_for_play();
                $this->load->view('MAIN_available_characters_view', $my_data);
            }
            
        }  ////// end of function display_available_characters
        ////////////////////////////////////////////////
        
        
        
        public function select_character_to_play(){
            
            //$character_choice = $this->input->post('characterChoice');
            
            //echo $character_choice;
            if ($this->_character_to_play_validate() === FALSE) {
                     $this->index();
                     return;
                 }
                 
            $this->load->model('setup_data_model');
            $this->setup_data_model->commit_character_to_play();
            
            $this->index();

            //echo "Completed function create_family! Now should run generate_family_members";
            //$this->generate_family_members();
            
            
        }   ////// end of function index
        ////////////////////////////////////////////////
        
        public function play_this_character() {
            
            
            $data = $this->input->post('whichCharacter');
            $_SESSION["active_character"] = $data;
            
            //echo $data;
            //$this->load->view('MAIN_character_view');
            redirect('/character');
            
            
        }  ////// end of function play_this_character
        ////////////////////////////////////////////////
        
        public function stop_playing_this_character() {
            
            //$data = $this->input->post('whichCharacter');
            //echo $data;
            $this->load->model('update_models');
            $this->update_models->stop_playing_character();
            
            $this->index();
            
        }  ////// end of function play_this_character
        ////////////////////////////////////////////////
        
        
         
         
         
         
        
        


        
         
         
         
         private function _character_to_play_validate() {
         
             $this->form_validation->set_rules('characterChoice', 'characterChoice', 
                     'required|min_length[1]|numeric|max_length[16]|xss_clean|trim');
             
             return $this->form_validation->run();
             
         } ////// end of private function _family_name_validate
         ///////////////////////////////////////////////
         
         
         
                 
         
         
         
    }    /////////////// END: class Main_interface extends CI_Controller 
    
}  //// END:  if (isset($_SESSION["user"]) && !empty($_SESSION["user"]))
else {
   // deny, log, redirect
    
    //$this->load->helper('url');
    //redirect('/dynasties2/start', 'location');
    
}


